<?php
declare (strict_types = 1);

namespace app\api\model;

use app\common\model\Api;
use think\Model;

/**
 * @mixin think\Model
 */
class UserIndex extends Api
{
    //登录检测
    public function usercheck($openid,$token,$pid = '')
    {
        $data = self::where('wx_openid','=', $openid)->find();
        $n_data = [];
        if (empty($data)) {
            if(!empty($pid)){
                $n_data['parent_id'] = $pid;
            }
            $sj = [
                'wx_openid' => $openid,
                'access_token' => $token
            ];
            $sj = array_merge($n_data,$sj);
            $data  = $sj;
            self::save($sj);
            
            $data['id'] = $this->id*1;
            // $token = $this->token();
            // $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token";
            // $p = [
            //     'page' => '',
            //     "scene" => (string)$data['id']
            // ];
            // $data_S = json_encode($p);
            // $res = curl_send([
            //     'url'=>$url,
            //     'method'=>'post',
            //     'data'=>$data_S
            // ]);
            //通过返回的参数保存在本地；
            // pr($res['content']);
            // die;
            // file_put_contents('code/'."wx".$data['id'].".png", $res['content']);
            // $qrcode = 'code/'."wx".$data['id'].".png";
            // self::where('id','=',$data['id'])->update(['qrcode'=>$qrcode]);
        } else {
            $data = $data->toArray();
            $list = ['access_token' => $token];
            if($data['parent_id'] == 0 && !empty($pid)){
                $list['parent_id'] = $pid;
            }
            if(empty($data['qrcode'])){
                //生成微信二维码
                // $token = $this->token();
                // $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$token";
                // $p = [
                //     'page' => '',
                //     "scene" => (string)$data['id']
                // ];
                // pr($p);
                // exit;
                // $data_S = json_encode($p);
                // $res = curl_send([
                //     'url'=>$url,
                //     'method'=>'post',
                //     'data'=>$data_S
                // ]);
                //通过返回的参数保存在本地；
                // pr($res['content']);
                // die;
                // file_put_contents('code/'."wx".$data['id'].".png", $res['content']);
                // $list['qrcode'] = 'code/'."wx".$data['id'].".png";
            }
            self::where('id','=',$data['id'])->update($list);
        }
        return self::where('id','=', $data['id'])->find();
    }
    //获取微信小程序后台AccessToken
    public function token()
    {
        // if (!Cache::get('token') || (!Cache::get('time') || Cache::get('time') < time())) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3b2b8fd75568542c&secret=1e6e104ec2e311c61bfed1d6ce0af717";
            $curl = curl_send(['url' => $url]);
            $token = json_decode($curl['content'])->access_token;
        //     Cache::set('token', $token);
        //     Cache::set('time', time() + 7200);
        // } else {
        //     $token = Cache::get('token');
        // }
        return $token;
    }
}
